package com.zch.action;

import java.util.List;
import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.PrincipalArea;
import com.zch.dao.model.PrincipalAreaExample;
import com.zch.service.PrincipalAreaService;

@RequestMapping("principalArea")
@Controller
@LoginFilter
public class PrincipalAreaController extends CoreController{

	@Autowired
	private PrincipalAreaService principalAreaService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
	    if(name==null){
	        name="";
	    }
		PaginationSupport.byPage(page, rows);
		PrincipalAreaExample example = new PrincipalAreaExample();
		example.createCriteria().andNameLike("%"+name+"%");
		example.setOrderByClause("create_time desc");
		List<PrincipalArea> list = principalAreaService.selectByExample(example);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(PrincipalArea principalArea){
		if (StringUtils.isBlank(principalArea.getId())){
			principalAreaService.insert(principalArea);
		}else{
			principalAreaService.updateByPrimaryKeySelective(principalArea);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		PrincipalArea principalArea = principalAreaService.selectByPrimaryKey(id);
		return JSONUtils.serialize(principalArea);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			principalAreaService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/myList")
    @ResponseBody
    public String MyList(){
        PrincipalAreaExample example = new PrincipalAreaExample();
        example.createCriteria();
        example.setOrderByClause("create_time desc");
        List<PrincipalArea> list = principalAreaService.selectByExample(example);
        return JSONUtils.serialize(list);
    }

}